package best.driver.android.View;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import best.driver.android.Beans.OrderAddresses;
import best.driver.android.Beans.OrderStatus;
import best.driver.android.R;
import butterknife.ButterKnife;
import butterknife.InjectView;

import java.util.ArrayList;
import java.util.List;

public class PhoneAdapter extends BaseAdapter {
    private LayoutInflater mInflater;

    private Context mContext;

    private List<OrderAddresses> mAddressItems = new ArrayList<OrderAddresses>();

    public PhoneAdapter(Context context) {
        mContext = context;
        mInflater = LayoutInflater.from(context);
    }

    public void setData(List<OrderAddresses> list) {
        mAddressItems.clear();
        for(OrderAddresses address:list) {
            if(!TextUtils.isEmpty(address.phone)) {
                mAddressItems.add(address);
            }
        }
        if(list == null || list.size() == 0) {
            return;
        }
        notifyDataSetChanged();
    }


    @Override
    public int getCount() {
        return mAddressItems.size();
    }

    @Override
    public OrderAddresses getItem(int position) {
        return mAddressItems.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView != null) {
            holder = (ViewHolder) convertView.getTag();
        } else {
            convertView = mInflater.inflate(R.layout.item_phone, parent, false);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        }

        setConvertView(holder, position);
        return convertView;
    }

    private void setConvertView(ViewHolder holder, int position) {
        final OrderAddresses item = getItem(position);
        if(!TextUtils.isEmpty(item.address)) {
            if(OrderStatus.SHIPPER_ADDRESS.equals(item.flag)) {
                holder.mTvAddress.setText("提货地："+item.address);
                holder.mTvAddress.setVisibility(View.VISIBLE);
            } else if(OrderStatus.RECEIVER_ADDRESS.equals(item.flag)){
                holder.mTvAddress.setText("卸货地："+item.address);
                holder.mTvAddress.setVisibility(View.VISIBLE);
            } else if("MAKER".equals(item.flag)) {
                holder.mTvAddress.setVisibility(View.GONE);
            } else {
                holder.mTvAddress.setVisibility(View.GONE);
            }
        } else {
            holder.mTvAddress.setText("");
            holder.mTvAddress.setVisibility(View.GONE);
        }

        if(!TextUtils.isEmpty(item.phone)) {
            holder.mTvPhone.setText(item.phone);
            if("MAKER".equals(item.flag)) {
                holder.mTvPhone.setText("下单人：" + item.phone);
            }
        } else {
            holder.mTvPhone.setText("");
        }

        if(!TextUtils.isEmpty(item.name)) {
            holder.mTvName.setText(item.name);
        } else {
            holder.mTvName.setText("");
        }

        holder.mIvPhone.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (!TextUtils.isEmpty(item.phone)) {
                    Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:" + item.phone));
                    mContext.startActivity(intent);
                }
            }
        });
    }

    static class ViewHolder {

        @InjectView(R.id.iv_phone)
        ImageView mIvPhone;

        @InjectView(R.id.tv_phone)
        TextView mTvPhone;

        @InjectView(R.id.tv_name)
        TextView mTvName;

        @InjectView(R.id.tv_address)
        TextView mTvAddress;

        ViewHolder(View view) {
            ButterKnife.inject(this, view);
        }
    }
}
